import http from './request'
import store from '@/store'
import { BASE_URL } from './request/config'

//模拟登录
export const handleLogin = (id) => http.get('/api/wx/manual/' + id)
// 发送消息
let sendMessage = (data, onSuccess, onError) => {
  return wx.request({
    url: BASE_URL + '/api/chat/message',
    data: data,
    method: 'POST',
    header: {
      Authorization: store.state.auth.token,
    },
    timeout: 30000,
    responseType: 'arraybuffer',
    enableChunked: true,
    success: (res) => onSuccess && onSuccess(res),
    fail: (err) => onError && onError(err),
  })
}

// #ifdef H5
sendMessage = (data) => {
  console.log('h5调接口', BASE_URL)
  return fetch(BASE_URL + '/api/chat/message', {
    method: 'POST',
    body: JSON.stringify(data),
    headers: {
      Authorization: store.state.auth.token,
      'Content-Type': 'application/json',
    },
  })
}
// #endif

export const uploadFile = (path) => {
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: BASE_URL + '/api/v1/file/upload',
      filePath: path,
      name: 'file',
      header: {
        Authorization: store.state.auth.token,
        'Content-Type': 'multipart/form-data',
      },
      formData: {
        file_type: 2,
      },
      success(res) {
        const data = JSON.parse(res.data)
        if (data.code === 200) {
          resolve(data.data)
        } else {
          reject(data.msg)
        }
      },
      fail: (err) => reject(err),
    })
  })
}

// 微信登录
export const wxLogin = (data) => http.post('/v1/wx/sapp/login', data)
// 公共配置
export const getCommonSetting = () => http.get('/api/common/setting')

// 发送验证码
export const sendSms = (data) => http.post('/api/user/verify-code', data)
// 注册
export const register = (data) => http.post('/api/user/register', data)
// 登录
export const login = (data) => http.post('/api/user/login', data)
// 忘记密码
export const forgetPassword = (data) => http.post('/api/user/forget-password', data)
// 获取用户信息
export const getUserInfo = (data) => http.post('/api/v1/user/info', data)
// 修改用户信息
export const updateUserInfo = (data) => http.post('/api/v1/user/info/update', data)
// 获取角色模型列表
export const getRoleList = () => http.get('/api/model/list')

// 总结上下文
export const summaryContext = (message) =>
  http.post('/api/v1/chat/message/ask', { message, type: '2' })
// 总结标题
export const summaryTitle = (message) => http.post('/api/chat/helper', { message, type: '1' })
// 兑换卡密
export const exchangeCard = (data) => http.post('/api/user/cd-key/redeem', data)
// 获取邀请列表
export const getInviteList = () => http.get('/api/user/invited-users')

/**
 * 聊天会话
 */
// 聊天会话 - 删除
export const conversationDelete = (message) =>
  http.post('/api/v1/chat/conversation/delete', message)
// 聊天会话 - 列表
export const conversationList = (data) => http.post('/api/v1/chat/conversation/list', data)
// 聊天会话 - 新建
export const conversationNew = (data) => http.post('/api/v1/chat/conversation/new', data)
// 聊天会话 - 修改
export const conversationUpdate = (data) => http.post('/api/v1/chat/conversation/update', data)
// 聊天内容 - 提问回答
export const chatMessageAsk = (data) => http.post('/api/v1/chat/message/ask', data)
// 聊天内容 - 列表
export const chatMessageList = (data) => http.post('/api/v1/chat/message/list', data)
// 聊天模型 - 列表
export const chatModelList = (data) => http.post('/api/v1/chat/model/list', data)

// 用户手机号 - 通过小程序获取
export const getMobile = (data) => http.post('/api/v1/user/wx/mobile', data)
// 用户设置-修改
export const userSettingInfoUpdate = (data) => http.post('/api/v1/user/setting/update', data)
// 用户设置-获取
export const userSettingInfo = (data) => http.post('/api/v1/user/setting/info', data)

// 分片上传
export const fileOss = (data) => http.post('/api/v1/file/oss', data)
// 客服信息
export const supportInfo = (data) => http.get('/v1/support/info', data)

// 分享相关
export const chatShareNew = (data) => http.post('/api/v1/chat/share/new', data)
export const chatShareList = (data) => http.post('/api/v1/chat/share/list', data)
export const chatShareUpdate = (data) => http.post('/api/v1/chat/share/update', data)
// 聊天详情
export const shareInfo = (data) =>
  http.get(`/v1/public/message/share/info?share_uuid=${data.share_uuid}`)
// 继续聊天
export const newFrom_share = (data) => http.post('/api/v1/chat/conversation/new/from_share', data)
export { sendMessage }
